﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UnitTests.Framework
{
	internal class StreamReadWriter : IDisposable
	{
		private StreamReader _reader;
		private StreamWriter _writer;
		private bool _disposed = false;

		public StreamReadWriter(Stream stream)
		{
			_reader = new StreamReader(stream);
			_writer = new StreamWriter(stream);
			_writer.AutoFlush = true;
		}

		public string WriteReadLine(string format)
		{
			_writer.WriteLine(format);
			return _reader.ReadLine();
		}

		public string WriteReadLine(string format, params string[] arguments)
		{
			_writer.WriteLine(format, arguments);
			return _reader.ReadLine();
		}

		public void WriteLine(string format)
		{
			_writer.WriteLine(format);
		}

		public void WriteLine(string format, params string[] arguments)
		{
			_writer.WriteLine(format, arguments);
		}

		public string ReadLine()
		{
			return _reader.ReadLine();
		}


		#region IDisposable Members
		/// <summary>
		/// Disposes all managed and unmanaged resources
		/// </summary>
		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Disposes all unmanaged resources and, optionally, the managed resources
		/// </summary>
		/// <param name="disposing">If true managed resources will be disposed</param>
		protected virtual void Dispose(bool disposing)
		{
			if (!_disposed)
			{
				if (disposing)
				{
					// Managed Resources
					_writer.Flush();
					_writer.Close();
					_reader.Close();
				}

				// Unmanaged Resources
			}

			_disposed = true;
		}
		#endregion
	}
}
